package cn.com.ccxe.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public abstract class BasciAction extends HttpServlet {

	private static final long 	serialVersionUID = -3499112329491458188L;

	@Override
	protected void doPost(HttpServletRequest req,
						  HttpServletResponse resp)
			  throws ServletException,
				     IOException {
		dispatch(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req,
						 HttpServletResponse resp)
			  throws ServletException,
				     IOException {
		dispatch(req, resp);
	}

	/**
	 * 错误处理
	 * @param req
	 * @param resp
	 * @param errorMessage
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void error(HttpServletRequest req,
						 HttpServletResponse resp,
						 String successMessage)
			  throws ServletException,
				     IOException {
		req.setAttribute("message", successMessage);
		forward(req, resp, "/common/success.jsp");
	}
	/**
	 * 错误处理
	 * @param req
	 * @param resp
	 * @param errorMessage
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void susscess(HttpServletRequest req,
			HttpServletResponse resp,
			String successMessage)
	throws ServletException,
	IOException {
		req.setAttribute("message", successMessage);
		forward(req, resp, "/common/success.jsp");
	}

	/**
	 * 跳转
	 * @param req
	 * @param resp
	 * @param path
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void forward(HttpServletRequest req,
						   HttpServletResponse resp,
						   String path
						   ) 
			  throws ServletException,
			  		 IOException {
		if(StringUtils.isEmpty(path)) {
			throw new RuntimeException("没有获取到路径！！！");
		}
		RequestDispatcher dispatcher = req.getRequestDispatcher(path+"");
		dispatcher.forward(req, resp);
	}
	/**
	 * 重定向
	 * @param req
	 * @param resp
	 * @param path
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void redirect(HttpServletRequest req,
						   HttpServletResponse resp,
						   String path
						   ) 
			  throws ServletException,
			  		 IOException {
		if(StringUtils.isEmpty(path)) {
			throw new RuntimeException("没有获取到路径！！！");
		}
		resp.sendRedirect(path);
	}
	/**
	 * 跳转
	 * @param req
	 * @param resp
	 * @param path
	 * @param isClean 是否需要清除某些参数
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void forward(HttpServletRequest req,
							HttpServletResponse resp,
							String path,
							boolean isClean) 
			throws ServletException,
				   IOException {
		if(StringUtils.isEmpty(path)) {
			throw new RuntimeException("没有获取到路径！！！");
		}
		if(isClean) {
			RequestDispatcher dispatcher = req.getRequestDispatcher(path+"?method=");
			dispatcher.forward(req, resp);
		}else {
			RequestDispatcher dispatcher = req.getRequestDispatcher(path);
			dispatcher.forward(req, resp);
		}
	}
	/**
	 * 将Session设置成过期。
	 * @param req
	 */
	protected void invalidateSession(HttpServletRequest req) {
		req.getSession().invalidate();
	}
	
	/**
	 * 分发请求<br/>
	 *  分发处理是根据属性method的值来进行分发。现支持如下两种形式，
	 *  比如要调用add方法：
	 *  <ol>
	 *  	<li>在url串后面直接添加<br>
	 *  	  	 如：<code>http://www.example.com?method=add</code>
	 *  	</li><br>
	 *  	<li>在表单中添加表单控件(请将"["、"]"替换成"<"、">")<br>
	 *  		如：<code>[input type='hidden' id='method' name='method' value='add'/]</code>
	 *  	</li>
	 *  </ol>
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected abstract void dispatch(HttpServletRequest req,
									 HttpServletResponse resp) 
					   throws ServletException,
					   		  IOException;

}
